/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;

import java.util.Vector;
import java.sql.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;


public class Main extends JFrame implements ActionListener{

    public Statement stmt1, stmt2, stmt3;
    public PreparedStatement pstmt;
    public ResultSet rset, rset2, rset3;
    public String query1, query2, query3;
    private JButton listbroken;
    private JButton addequip;
    private JButton viewequips;
    private JButton searchequip;
    private JButton searchmember;
    private JButton viewmembers;
    private JButton addmember;
    private JButton viewdebts;
    private JComboBox comboequip;
    private JComboBox combomem;
    private JLabel filterequip;
    private JLabel header;
    private JLabel gymfaci;
    private JLabel filtermember;
    private JPanel membertab;
    private JPanel equiptab;
    private JScrollPane formember;
    private JTabbedPane tabbedpane;
    private JTextField searchboxM;
    private JTextField searchboxE;
    private JTextPane welcome;
    private JOptionPane invalidsearchE;
    private JButton logout;

    public Main()
    {
        super("Brickroad Fitness Membership and Debt Tracking System");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        initComponents();
    }

    private void initComponents() {

        String[] elist = {"ID", "Name"};
        String[] mlist = {"ID", "Name"};

        tabbedpane = new JTabbedPane();
        membertab = new JPanel();
        filtermember = new JLabel();
        combomem = new JComboBox(mlist);
        searchboxM = new JTextField();
        searchmember = new JButton();
        viewmembers = new JButton();
        addmember = new JButton();
        viewdebts = new JButton();
        formember= new JScrollPane();
        welcome = new JTextPane();
        logout = new JButton();

        equiptab = new JPanel();
        searchboxE = new JTextField();
        filterequip = new JLabel();
        comboequip = new JComboBox(elist);
        listbroken = new JButton();
        addequip = new JButton();
        viewequips = new JButton();
        searchequip = new JButton();
        gymfaci = new JLabel();
        header = new JLabel();

        logout.addActionListener(this);
        viewdebts.addActionListener(this);
        searchmember.addActionListener(this);
        viewmembers.addActionListener(this);
        addmember.addActionListener(this);
        listbroken.addActionListener(this);
        addequip.addActionListener(this);
        viewequips.addActionListener(this);
        searchequip.addActionListener(this);

        tabbedpane.setBackground(new Color(255, 255, 102));
        tabbedpane.setFont(new Font("Rockwell Extra Bold", 0, 14)); // NOI18N

        membertab.setBackground(new Color(255, 255, 102));
        membertab.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        filtermember.setFont(new Font("Rockwell Extra Bold", 0, 14)); // NOI18N
        filtermember.setForeground(new Color(73, 29, 2));
        filtermember.setText("Filter:");

        combomem.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        combomem.setModel(new DefaultComboBoxModel(new String[] { "ID", "Name" }));
        combomem.setCursor(new Cursor(HAND_CURSOR));

        searchboxM.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        //searchboxM.setText("Enter Member ID or name");

        searchmember.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        searchmember.setText("SEARCH");
        searchmember.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        searchmember.setCursor(new Cursor(HAND_CURSOR));

        viewmembers.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        viewmembers.setText("View All Members");
        viewmembers.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        viewmembers.setCursor(new Cursor(HAND_CURSOR));

        addmember.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        addmember.setText("Add Member");
        addmember.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        addmember.setCursor(new Cursor(HAND_CURSOR));

        viewdebts.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        viewdebts.setText("View All Debts");
        viewdebts.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        viewdebts.setCursor(new Cursor(HAND_CURSOR));

        logout.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        logout.setText("LOGOUT");
        logout.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        logout.setCursor(new Cursor(HAND_CURSOR));

        welcome.setEditable(false);
        welcome.setFont(new Font("Rockwell", 1, 12)); // NOI18N
        welcome.setForeground(new Color(125, 46, 7));
        welcome.setText("                                                           VISION\n\nCREATE awareness, and then develop the consciousness of the Filipino\nto make health and fitness a major part of his/her lifestyle.  \n\nELEVATE the industry by offering services and products that are second\nto none.  \n\nEDUCATE the Filipino people about the value of health and fitness, and \nreach to as many people as we can with the resources we have.\nBe recognized with our service and also as providers of knowledge and \nskills.");
        formember.setViewportView(welcome);

        GroupLayout membertabLayout = new GroupLayout(membertab);
        membertab.setLayout(membertabLayout);
        membertabLayout.setHorizontalGroup(
            membertabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(membertabLayout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addGroup(membertabLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(viewdebts, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addmember, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(viewmembers, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(logout, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(GroupLayout.Alignment.TRAILING, membertabLayout.createSequentialGroup()
                        .addComponent(filtermember)
                        .addGap(18, 18, 18)
                        .addComponent(combomem, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)))
                .addGap(43, 43, 43)
                .addGroup(membertabLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addGroup(membertabLayout.createSequentialGroup()
                        .addComponent(searchboxM, GroupLayout.PREFERRED_SIZE, 314, GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(searchmember, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE))
                    .addComponent(formember))
                .addContainerGap(171, Short.MAX_VALUE))
        );
        membertabLayout.setVerticalGroup(
            membertabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(membertabLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(membertabLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(filtermember)
                    .addComponent(combomem, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchboxM, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchmember, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                .addGroup(membertabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(membertabLayout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(viewdebts, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(viewmembers, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(addmember, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(logout, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
                    .addGroup(membertabLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(formember, GroupLayout.PREFERRED_SIZE, 233, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        tabbedpane.addTab("Member", membertab);

        equiptab.setBackground(new Color(255, 255, 102));
        equiptab.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        searchboxE.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        //searchboxE.setForeground(new Color(204, 204, 204));
        //searchboxE.setText("Enter Equipment ID or name");

        filterequip.setFont(new Font("Rockwell Extra Bold", 0, 14)); // NOI18N
        filterequip.setForeground(new Color(101, 40, 7));
        filterequip.setText("Filter:");


        comboequip.setFont(new Font("Tahoma", 0, 14)); // NOI18N
        comboequip.setCursor(new Cursor(HAND_CURSOR));

        listbroken.setFont(new Font("Century Gothic", 1, 12)); // NOI18N
        listbroken.setText("List Broken Equipments");
        listbroken.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        listbroken.setCursor(new Cursor(HAND_CURSOR));

        addequip.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        addequip.setText("Add New Equipment");
        addequip.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        addequip.setCursor(new Cursor(HAND_CURSOR));

        viewequips.setFont(new Font("Century Gothic", 1, 14)); // NOI18N
        viewequips.setText("View All Equipments");
        viewequips.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        viewequips.setCursor(new Cursor(HAND_CURSOR));

        searchequip.setFont(new Font("Century Gothic", 1, 12)); // NOI18N
        searchequip.setText("SEARCH");
        searchequip.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        searchequip.setCursor(new Cursor(HAND_CURSOR));

        gymfaci.setIcon(new ImageIcon(getClass().getResource("/gym 2.jpg"))); // NOI18N
        gymfaci.setText("Gym Facility");

        GroupLayout equiptabLayout = new GroupLayout(equiptab);
        equiptab.setLayout(equiptabLayout);
        equiptabLayout.setHorizontalGroup(
            equiptabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(equiptabLayout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(filterequip)
                .addGap(38, 38, 38)
                .addComponent(comboequip, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(searchboxE, GroupLayout.PREFERRED_SIZE, 345, GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(searchequip, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE))
            .addGroup(equiptabLayout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addGroup(equiptabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(viewequips, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
                    .addComponent(listbroken, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
                    .addComponent(addequip, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE))
                .addGap(84, 84, 84)
                .addComponent(gymfaci, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE))
        );
        equiptabLayout.setVerticalGroup(
            equiptabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(equiptabLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(equiptabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(equiptabLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(filterequip))
                    .addComponent(comboequip, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchboxE, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchequip, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(equiptabLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(equiptabLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(viewequips, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(listbroken, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(addequip, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
                    .addComponent(gymfaci, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)))
        );

        tabbedpane.addTab("Equipments", equiptab);

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(tabbedpane, 0, 0, Short.MAX_VALUE)
            .addComponent(header, GroupLayout.PREFERRED_SIZE, 889, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabbedpane, GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE))
        );
        cnd a = new cnd();

        try{
                        long currentTime = System.currentTimeMillis();
                        java.sql.Date date = new java.sql.Date(currentTime - (currentTime % 86400000));
                        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        //java.util.Date parsedUtilDate = sdf.parse(sdf.format(date));
                        //java.sql.Date sqlDate= new java.sql.Date(parsedUtilDate.getTime());
                        a.connect();
                        System.out.println(date);

                        query1 = "SELECT mExpiration FROM `member` where mID = 1";
                        stmt1 = a.con1.createStatement();
                        rset = stmt1.executeQuery(query1);
                        rset.next();
                        System.out.println(rset.getDate("mExpiration"));
                        //float bal = rset.getFloat("mBalance");
                        //float add = Integer.parseInt(amount.getText());
                        System.out.println("Updating...");
                        query2 = "UPDATE `member`" +
                                "SET `mStatus` = \"" + "Not a Member" + "\" WHERE `mExpiration` <" +date;
                        stmt2 = a.con1.createStatement();
                        stmt2.executeUpdate(query2);
                        System.out.println("Updated");
                        //rset.next();
                            
                        
                        a.disconnect();
                     }
                    catch(Exception z){
                             JOptionPane invalidsearchE = new JOptionPane();
                             invalidsearchE.showMessageDialog(rootPane, "ERROR: " +z.getMessage());
                             System.out.println("Error copying records from member table. " + z.getMessage());
                    }
        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();

        //DOESN'T WORK YET.
        if (event == searchequip)
        {
            if (searchboxE.getText().equals(""))
            {
                invalidsearchE = new JOptionPane();
                invalidsearchE.showMessageDialog(rootPane, "Enter the name/id of the equipment");
            }
            else
            {
                int eq;
                cnd a = new cnd();
                try {
                    a.connect();
                    String equip = searchboxE.getText();
                    if (comboequip.getSelectedIndex()==0)
                    {
                        try{
                        eq = Integer.parseInt(equip);
                        pstmt = a.con1.prepareStatement("SELECT * FROM `equipment` where `eqID` = ?");
                        pstmt.setInt(1, eq);
                        rset = pstmt.executeQuery();
                        }
                        catch(Exception x)
                        {
                            invalidsearchE.showMessageDialog(rootPane, "ERROR: Only integers are acceptable input. Recheck input and filters. " + x.getMessage());
                        }
                    }
                    else if (comboequip.getSelectedIndex() == 1)
                    {
                        try{
                        pstmt = a.con1.prepareStatement("SELECT * FROM `equipment` where `eqName` LIKE ?");
                        pstmt.setString(1, "%"+equip+"%");
                        rset = pstmt.executeQuery();
                        }
                        catch (Exception ex)
                        {
                            invalidsearchE.showMessageDialog(rootPane, "ERROR: " + ex.getMessage());
                        }
                    }

                    if(rset.next() == false)
                    {
                        invalidsearchE = new JOptionPane();
                        invalidsearchE.showMessageDialog(rootPane, "Entry not found. Verify your query and the filter used.");
                    }
                    else
                    {
                        dispose();
                        new SearchEquip(comboequip.getSelectedIndex(), equip);
                    }
                    a.disconnect();
                }
                catch (Exception ex)
                {
                    System.out.println("Error copying records from equipment table. " + ex.getMessage());
                }
            }
        }
        else if (event == addequip)
        {
            new AddEquip();
        }
        else if (event == listbroken)
        {
            new ListBroken();
        }
        else if (event == viewequips)
        {
            new ViewEquip();
        }
        else if (event == viewdebts)
        {
            new ViewDebts();
        }
        else if (event == addmember)
        {
            new AddMember();
        }
        else if (event == logout)
        {
            dispose();
            new Login();
        }
        else if (event == viewmembers)
        {
            dispose();
            new ViewMembers();
        }
        else if (event == searchmember)
        {
            if (searchboxM.getText().equals(""))
            {
                invalidsearchE = new JOptionPane();
                invalidsearchE.showMessageDialog(rootPane, "Enter the name/id of the member");
            }
            else
            {
                int mem;
                cnd a = new cnd();
                try {
                    a.connect();
                    String member = searchboxM.getText();
                    if (combomem.getSelectedIndex()==0)
                    {
                        try{
                        mem = Integer.parseInt(member);
                        pstmt = a.con1.prepareStatement("SELECT * FROM `member` where `mID` = ?");
                        pstmt.setInt(1, mem);
                        rset = pstmt.executeQuery();
                        }
                        catch(Exception x)
                        {
                            invalidsearchE.showMessageDialog(rootPane, "ERROR: Only integers are acceptable input. Recheck input and filters. " + x.getMessage());
                        }
                    }
                    else if (combomem.getSelectedIndex() == 1)
                    {
                        try{
                        pstmt = a.con1.prepareStatement("SELECT * FROM `member` where `mLastname` LIKE ? OR `mFirstname` LIKE ?");
                        pstmt.setString(1, "%"+member+"%");
                        pstmt.setString(2, "%"+member+"%");
                        rset = pstmt.executeQuery();
                        }
                        catch (Exception ex)
                        {
                            invalidsearchE.showMessageDialog(rootPane, "ERROR: " + ex.getMessage());
                        }
                    }

                    if(rset.next() == false)
                    {
                        invalidsearchE = new JOptionPane();
                        invalidsearchE.showMessageDialog(rootPane, "Entry not found. Verify your query and the filter used.");
                    }
                    else
                    {
                        dispose();
                        a.disconnect();
                        new SearchMember(combomem.getSelectedIndex(), searchboxM.getText());
                    }
                    //a.disconnect();
                }
                catch (Exception ex)
                {
                    invalidsearchE.showMessageDialog(rootPane, "ERROR: " + ex.getMessage());
                    System.out.println("Error copying records from member table. " + ex.getMessage());
                }
            }
        }
    }
}
